package helpers;

import java.util.Locale;
import java.util.ResourceBundle;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

public class UrlHelper{
	public static String getServerUrl(HttpServletRequest request) {
		String url = "";
		String serverName = request.getServerName();
		String port = Integer.toString(request.getServerPort());
		if (serverName.equals("localhost")) {
			url += "http://" + serverName + ":" + port;
		} else {
			url += "http://" + serverName;
		}
		return url;
	}
	
	public static Boolean checkDomain(String domain) {
        ResourceBundle resource = ResourceBundle.getBundle("language",
                        new Locale("en"));
        String re_domain = domain;

        if(!Pattern.matches("^[a-zA-Z0-9-]+([.][a-zA-Z_0-9-]+)+", re_domain))
        {
        	return false;
        }
        
        String[] listDomain = resource.getString("domain").split(" ");
        for (int i = 0; i < listDomain.length; i++) {
                re_domain = re_domain
                                .replaceAll("[.]{1}" + listDomain[i] + "$", "");
        }

        if (re_domain.length() != domain.length()) {
                return true;
        }

        return false;
	}
	
	public static Boolean checkSpecialDomain2(String domain) {
		ResourceBundle resource = ResourceBundle.getBundle("whitelist_domain",
                new Locale("en"));
		
		
        if (!Pattern.matches("^[a-zA-Z0-9-]+([.][a-zA-Z_0-9-]+)+", domain)) {
        	System.out.println("domain name not valid");
        	return false;
        }
        
        String re_domain = domain;
		String[] listDomain = resource.getString("domain").split(" ");
        for (int i = 0; i < listDomain.length; i++) {
        	re_domain = re_domain.replaceAll("[.]{1}" + listDomain[i] + "$", "");
        }
        
        if (re_domain.length() != domain.length()) {
            return true;
        }
        System.err.println("Domain don't in country find");
		return false;
	}
	
	public static Boolean checkSpecialDomain(String domain, String extension) {
		
        if (!Pattern.matches("^[a-zA-Z0-9-]+([.][a-zA-Z_0-9-]+)+", domain)) {
        	System.out.println("domain name not valid");
        	return false;
        }
        
        String re_domain = domain;
		String[] listDomain = extension.split(" ");
        for (int i = 0; i < listDomain.length; i++) {
        	re_domain = re_domain.replaceAll("[.]{1}" + listDomain[i] + "$", "");
        }
        
        if (re_domain.length() != domain.length()) {
            return true;
        }
        System.err.println("Domain don't in country find");
		return false;
	}
	
	public static Boolean checkIsSubDomain(String domain) {
		ResourceBundle resource = ResourceBundle.getBundle("language",
                new Locale("en"));
		String re_domain = domain;
		
		String[] listDomain = resource.getString("domain").split(" ");
        for (int i = 0; i < listDomain.length; i++) {
                re_domain = re_domain
                                .replaceAll("[.]{1}" + listDomain[i] + "$", "");
        }
        
        if (re_domain.length() != domain.length()) {
        	if (Pattern.matches("^[a-zA-Z0-9-]+$", re_domain.replace("www.", ""))) {
            	return false;
            }
        }
		
		return true;
	}
	
	public static void main(String[] args) {
		System.out.println(checkIsSubDomain("www.blog.hoanbn.us.org"));
	}
}